﻿#-*- coding:utf-8 -*-
from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from spider.items import SpiderItem

class spider(BaseSpider):
    name = "spider"
    allowed_domines = ["http://detail.zol.com.cn"]
    start_urls = []
    for id in range(1,14):
        start_urls.append("http://detail.zol.com.cn/notebook_index/subcate16_list_"+str(id)+".html")
    
    def parse(self, response):
        #print "enter in parse"
        items = []
        hxs = HtmlXPathSelector(response)
        urls = hxs.select('//div[@id="list"]/div[@class ="conlist"]/div[@class="item"]/div[@class="pic"]/a/@href').extract()
        url_harf = "http://detail.zol.com.cn"
        for url in urls:
            #print url
            url = url_harf + str(url) 
            items.append(self.make_requests_from_url(url).replace(callback=self.parse_middle))
        return items
    def parse_middle(self,response):
        print "enter in parse_middle" 
        hxs = HtmlXPathSelector(response)
        li = hxs.select('//ul[@class="nav"]/li/a/@href').extract()
        print "li value" , li[0]
        url_middle = "http://detail.zol.com.cn" + li[0]
        print url_middle ,"url_middle"
        items = []
        items.append(self.make_requests_from_url(url_middle).replace(callback=self.parse_goto))
        return items
        
    def parse_goto(self,response):
        print "enter in parse_goto "
        hxs = HtmlXPathSelector(response)
        print hxs,"hxssssssssssssssssssssssss"
        gotos = hxs.select('//table/tbody/tr/td[@class="more"]/p/a/@href').extract()
        #print gotos, "dfffffffffffffffffffffffffffffffffffffff"
        url_harf = "http://detail.zol.com.cn"
        for url_goto in gotos:
            url_goto = url_harf + str(url_goto)
            print url_ogto,"in goto you can do you can "
            where.extend([self.make_requests_from_url(url_goto).replace(callback=self.parse_middle)])
            #print url_ogto,"in goto you can do you can "
        return gotos
        
    def parse_end(self,response):
        print "enter in parse_end"